package com.jhhc.StormSurgeForecast.Calculate.util;

import java.io.*;
import java.util.Arrays;

/**
 * @author LYQ
 */
public class DosUtil {
    /**
     * windows
     */
    public static String executeCommands(String[] commands) {
        System.out.println(commands);
        StringBuilder commandBuilder = new StringBuilder();
        try {
            ProcessBuilder builder = new ProcessBuilder("cmd", "/k");
            builder.redirectErrorStream(true);
            Process process = builder.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            for (String command : commands) {
                commandBuilder.append(command);
                commandBuilder.append(System.lineSeparator());
            }
            process.getOutputStream().write(commandBuilder.toString().getBytes());
            process.getOutputStream().close();
            String line;
            commandBuilder = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                commandBuilder.append(line).append(",");
            }
            reader.close();
            process.destroy();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return commandBuilder.toString();
    }

    public static void executeCmd(String[] cmds) {
        System.out.println(Arrays.toString(cmds));
        Process proc = null;
        try {
            // 多条命令执行
            //String[] cmds = {"/bin/sh", "-c", "cp /home/admin/ceshi.sh /home/admin/files && cd /home/admin/files && pwd"};
            proc = Runtime.getRuntime().exec(cmds);
            proc.waitFor();

            InputStream in = proc.getInputStream();
            BufferedReader read = new BufferedReader(new InputStreamReader(in));
            String line = null;
            while((line = read.readLine()) != null){
                System.out.println(line);
            }
            in.close();
            read.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                if(null != proc) {
                    proc.destroy();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void execute(String cmd) {
        try {
            System.out.println(cmd);
            Process process = Runtime.getRuntime().exec(cmd);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            reader.close();
            process.waitFor();
            // 销毁进程以释放资源
            process.destroy();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void execute2(String path) {
        try {
            // 设置工作目录
            File workingDirectory = new File(path);

            // 设置要执行的命令
            String command = "sh sub.sh"; // 例如，在Unix/Linux系统中列出目录内容

            // 执行命令，并设置工作目录
            Process process = Runtime.getRuntime().exec(command, null, workingDirectory);

            // 处理进程输出（如果需要）
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            // 等待命令执行完成
            process.waitFor();
            // 获取退出值
            int exitValue = process.exitValue();
            System.out.println("Exit value: " + exitValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
